package cn.ticket.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Constants;
import cn.ticket.model.Staff;
import cn.ticket.model.Staffdata;
import cn.ticket.service.DepartmentService;
import cn.ticket.service.MainShowAuthorityService;
import cn.ticket.service.StaffDataService;
import cn.ticket.service.StaffService;
import cn.ticket.service.Utility;

@Controller
@RequestMapping("staff")
public class StaffController {
	@Autowired
	StaffService staffService;
	@Autowired
	StaffDataService staffDataService;
	@Autowired
	DepartmentService departmentService;
	@Autowired
	MainShowAuthorityService mainshowAuthorityService;
	@RequestMapping("")
	public String StaffIndex(){
		return "staff";
	}
	@RequestMapping(value="updatePassword",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String updatePassword(@RequestBody Map<String, String> args){
		Map<String, Object> result=new HashMap<>();
		String phone=args.get("phone");
//		phone=phone.toUpperCase();
		String password=args.get("password");
		String newPassword=args.get("newPassword");
		Staff staff=staffService.validateStaff(phone,password);	
		if (staff==null) {
			result.put("status", 1002);
			result.put("message", "验证失败,请检查原密码");
		}
		else {
			staff.setPassword(newPassword);
			if (staffService.updateByPrimaryKeySelective(staff)==1) {
				result.put("status", 1001);
			}
			else {
				result.put("status", 1002);
				result.put("message", "更新密码失败");
			}
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="initPassword",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String initPassword(@RequestBody Map<String, Object> args){
		Map<String, Object> result=new HashMap<>();
		Integer id=(Integer) args.get("id");
		String password=(String) args.get("password");		
		Staff staffResult=staffService.getByIdAndPassword(id, password);
		if (staffResult==null) {
			result.put("status", 1002);
			result.put("message", "验证失败,请检查原密码");
		}
		else {
			staffResult.setPassword("666666");
			if (staffService.updateByPrimaryKeySelective(staffResult)==1) {
				result.put("status", 1001);
			}
			else {
				result.put("status", 1002);
				result.put("message", "初始化密码失败");
			}
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="validate",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String validate(@RequestBody Map<String, String> args){
		String phone=args.get("phone");
	//	phone=phone.toUpperCase();
		String password=args.get("password");
		Map<String, Object> result=new HashMap<>();
		Staff staff=staffService.validateStaff(phone,password);			
		result.put("status", 1001);
		if (staff==null) {
			result.put("validateStatus", "error");
		}
		else {
			result.put("name", staff.getName());
			result.put("id", staff.getId());
			if (staff.getIseabled()==true) {
				result.put("validateStatus", "right");
			}
			else {
				result.put("validateStatus", "wrong");
			}
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="heartAuthority",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String heartValidate(@RequestBody Map<String, String> args,HttpServletRequest request) throws ParseException{
		String phone=args.get("phone");
	//	phone=phone.toUpperCase();
		String password=args.get("password");
		String appInfo=args.get("app");
		Map<String, Object> result=new HashMap<>();
		Staff staff=staffService.validateStaff(phone,password);				
		if (staff==null) {
			result.put("status", 1003);
		}
		else {
			String client_ip = "";
			if (request.getHeader("x-forwarded-for") == null) {
				client_ip =  request.getRemoteAddr();
			}else{
				client_ip = request.getHeader("x-forwarded-for");
			}
			staff.setLastlogindate11(new Date());
			if (appInfo!=null) {
				staff.setAppinfo(appInfo);
			}
			staff.setLastloginip(client_ip);
			staffService.updateByPrimaryKeySelective(staff);
			result.put("status", 1001);
			result.put("minVersion", Constants.MinVersion);
			result.put("latestVersion", Constants.LatestVersion);
			result.put("authority", mainshowAuthorityService.getAuthorityByLevel(staff.getLevel()));			
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getAll",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getAll(){
		Map<String, Object> result=new HashMap<>();
		List<Staff> staffs=staffService.getAll();
		if (!staffs.isEmpty()) {
			result.put("status", 1001);
			result.put("body", staffs);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getByKeywords",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByKeywords(@RequestBody Map<String, String> args){
		Map<String, Object> result=new HashMap<>();
		String keywords=args.get("keywords");
		List<Map<String, Object>> staffinfo=staffService.getByKeywords(keywords.trim());
		result.put("status", 1001);
		result.put("body", staffinfo);
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getByCount",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByCount(@RequestBody Map<String, String> args){
		int start=Integer.parseInt(args.get("start"));
		int count=Integer.parseInt(args.get("count"));
		Map<String, Object> result=new HashMap<>();
		List<Staff> staffs=staffService.getByCount(start, count);
		if (!staffs.isEmpty()) {
			result.put("status", 1001);
			result.put("body", staffs);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value="getByDepartmentId",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByDepartmentId(@RequestBody Map<String, String> args){
		int departmentId=Integer.parseInt(args.get("departmentId"));
		Map<String, Object> result=new HashMap<>();
		List<Staff> staffs=staffService.getByDepartment(departmentId);
		if (!staffs.isEmpty()) {
			result.put("status", 1001);
			result.put("body", staffs);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String update(@RequestBody Staff staff ){
		Integer id=staff.getId();
		String password=staff.getPassword();
		Map<String, Object> result=new HashMap<>();
		Staff staffResult=staffService.getByIdAndPassword(id, password);
		if (staffResult==null) {
			result.put("status", 1003);
			result.put("message", "用户验证失败");
			return Utility.gson.toJson(result);
		}
		staff.setName(staffResult.getName());
		staff.setOwncount(staffResult.getOwncount());
		staff.setIseabled(staffResult.getIseabled());
		staff.setRefundcount(staffResult.getRefundcount());
		staff.setRelativecount(staffResult.getRelativecount());
		if (!staffResult.getStatus().equals("0")&&!staffResult.getStatus().equals(staff.getStatus())) {
			result.put("status", 1002);
			result.put("message", "职工类别现仅限由在职改为离退休!");
			return Utility.gson.toJson(result);
		}
		else if (staffResult.getStatus().equals("4")&&!staffResult.getStatus().equals(staff.getStatus())) {
			result.put("status", 1002);
			result.put("message", "退休用户状态不可再改为在职!");
			return Utility.gson.toJson(result);
		}
		if (staffResult.getStatus().equals("1")&&staff.getStatus().equals("4")) {
			staff.setOwncount(0);
		}
		int num=staffService.updateByPrimaryKeySelective(staff);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
			result.put("message", "更新失败");
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="updateByAdmin",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String updateByAdmin(@RequestBody Staff staff ){
		Integer id=staff.getId();
		String password=staff.getPassword();
		Map<String, Object> result=new HashMap<>();
		Staff staffResult=staffService.getByIdAndPassword(id, password);
		if (staffResult==null) {
			result.put("status", 1003);
			result.put("message", "用户验证失败");
			return Utility.gson.toJson(result);
		}
		
		int num=staffService.updateByPrimaryKeySelective(staff);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
			result.put("message", "更新失败");
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insert(@RequestBody Staff staff ){		
		Map<String, Object> result=new HashMap<>();
		return Utility.gson.toJson(result);
//		int num=staffService.insertSelective(staff);
//		if (num==1) {
//			result.put("status", 1001);
//		}
//		else {
//			result.put("status", 1002);
//		}
//		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="insertByAdmin",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insertByAdmin(@RequestBody Staff staff ){		
		Map<String, Object> result=new HashMap<>();		
		int num=staffService.insertSelective(staff);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="delete",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String delete(@RequestBody Map<String, Integer> args ){		
		Map<String, Object> result=new HashMap<>();
		int num=staffService.deleteByPrimaryKey(args.get("id"));
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	
	
	@RequestMapping(value="getStaffInfoByKeyWords",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getStaffInfoByKeyWords(@RequestBody Map<String, String> args){
		Map<String, Object> result=new HashMap<>();
		String keywords=args.get("keywords");
		List<Map<String, Object>> staffinfo=staffService.getStaffInfoByKeyWords(keywords.trim());
		result.put("status", 1001);
		result.put("body", staffinfo);
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getStaffInfoByDepartment",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getStaffInfoByDepartment(@RequestBody Map<String, String> args){
		int departmentId=Integer.parseInt(args.get("departmentId"));
		Map<String, Object> result=new HashMap<>();
		List<Map<String, Object>> staffs=staffService.getStaffInfoByDepartment(departmentId);
		if (!staffs.isEmpty()) {
			result.put("status", 1001);
			result.put("body", staffs);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value="getStaffInfoByCount",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getStaffInfoByCount(@RequestBody Map<String, String> args){
		int start=Integer.parseInt(args.get("start"));
		int count=Integer.parseInt(args.get("count"));
		Map<String, Object> result=new HashMap<>();
		List<Map<String, Object>> staffs=staffService.getStaffInfoByCount(start, count);
		if (!staffs.isEmpty()) {
			result.put("status", 1001);			
			result.put("body", staffs);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getStaffInfoByStaffId",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getStaffInfoByStaffId(@RequestBody Map<String, String> args){
		int staffId=Integer.parseInt(args.get("staffId"));
		Map<String, Object> result=new HashMap<>();
		List<Map<String, Object>> staffs=staffService.getStaffInfoByStaffId(staffId);
		if (!staffs.isEmpty()) {
			result.put("status", 1001);
			result.put("body", staffs);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getStaffCount",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getStaffCount(){	
		Map<String, Object> result=new HashMap<>();
		int staffCount=staffService.getStaffCount();		
		result.put("status", 1001);
		result.put("body", staffCount);		
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value="importStaff",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String importStaff(){
		List<Staffdata> staffdatas=staffDataService.getAll();
		int count=0;
		for (Staffdata staffdata : staffdatas) {
			String name=staffdata.getName();
			String number=staffdata.getNumber();
			String work=staffdata.getWork();
			String department=staffdata.getDepartment();
			int departmentId=departmentService.getIdByName(department.trim());
			Staff staff=new Staff();
			staff.setIseabled(true);
			staff.setPassword("666666");
			staff.setName(name);
			staff.setNumber(number);
			staff.setDepartmentid(departmentId);
			if (work.equals("正式职工")) {
				staff.setStatus("0");
			}
			else if (work.equals("招聘")) {
				staff.setStatus("1");
			}
			else if (work.equals("劳务派遣")) {
				staff.setStatus("2");
			}
			else if (work.equals("退休返聘")) {
				staff.setStatus("3");
			}
			else if (work.equals("离退休")) {
				staff.setStatus("4");
			}
			else {
				staff.setStatus("5");
			}
			count=staffService.insertSelective(staff)+count;
		}
		return Utility.createJsonMsg(1001, "success", count);
	}
	@RequestMapping(value="getStaffCountByDepartment",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getStaffCountByDepartment(@RequestBody Map<String, String> args){	
		int departmentId=Integer.parseInt(args.get("departmentId"));
		Map<String, Object> result=new HashMap<>();
		int staffCount=staffService.getStaffCountByDepartment(departmentId);		
		result.put("status", 1001);
		result.put("body", staffCount);		
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getStaffInfoByDepartmentAndCount",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getStaffInfoByDepartmentAndCount(@RequestBody Map<String, String> args){
		int start=Integer.parseInt(args.get("start"));
		int count=Integer.parseInt(args.get("count"));
		int departmentId=Integer.parseInt(args.get("departmentId"));
		Map<String, Object> result=new HashMap<>();
		List<Map<String, Object>> staffs=staffService.getStaffInfoByDepartmentAndCount(departmentId, start, count);
		if (!staffs.isEmpty()) {
			result.put("status", 1001);			
			result.put("body", staffs);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	/*
	 * 科室主任获取待审核的职工数据
	 * */
	@RequestMapping(value = "getAuditingStaffInfoByAuditor", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String getAuditingStaffInfoByAuditor(@RequestBody Map<String, String> args){
		Map<String, Object> result = new HashMap<>();
		String phone = args.get("phone");
		String password = args.get("password");
		String staffId = args.get("staffId");
		
		
		Staff staff = staffService.validateStaff(phone, password);
		
		if (staff == null || staff.getId() != Integer.parseInt(staffId)) {
			result.put("status", 1002);
			result.put("message", "登录账户验证失败!");
			return Utility.gson.toJson(result);
		}
		if (staff.getLevel()==1) {
			result.put("status", 1002);
			result.put("message", "无权限!");
			return Utility.gson.toJson(result);
		}		
		List<Map<String, Object>> staffInfos=staffService.getAuditingStaffInfoByDepartmentId(staff.getDepartmentid());
		if (staffInfos.isEmpty()) {
			result.put("status", 1002);
			result.put("message", "无职工信息修改申请!");
			return Utility.gson.toJson(result);
		}
		return null;
	}
	/**
	 * 科室主任审批资料修改请求
	 * **/
	@RequestMapping(value = "auditStaffInfoByAuditor", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String auditStaffInfoByAuditor(@RequestBody Map<String, String> args,HttpServletRequest request){
		String client_ip = "";
		if (request.getHeader("x-forwarded-for") == null) {
			client_ip =  request.getRemoteAddr();
		}else{
			client_ip = request.getHeader("x-forwarded-for");
		}
		Map<String, Object> result = new HashMap<>();
		String phone = args.get("phone");
		String password = args.get("password");
		String staffId = args.get("staffId");
		int auditingStaffId=Integer.parseInt(args.get("auditingStaffId"));
		Staff auditingStaff=staffService.selectByPrimaryKey(auditingStaffId);
		Staff staff = staffService.validateStaff(phone, password);
		if (staff == null || staff.getId() != Integer.parseInt(staffId)) {
			result.put("status", 1002);
			result.put("message", "登录账户验证失败!");
			return Utility.gson.toJson(result);
		}
		if (staff.getPassword().equals("666666")) {
			result.put("status", 1002);
			result.put("message", "默认密码无审核权限,请修改密码!");
			return Utility.gson.toJson(result);
		}
		if (staff.getLevel()==1) {
			result.put("status", 1002);
			result.put("message", "无权限!");
			return Utility.gson.toJson(result);
		}	
		auditingStaff.setIsneedcheck(false);
		auditingStaff.setIseabled(true);
		if (staffService.updateByPrimaryKeySelective(auditingStaff)==1) {
			result.put("status", 1001);
			return Utility.gson.toJson(result);
		}
		result.put("status", 1002);
		result.put("message", "审核失败!");
		return Utility.gson.toJson(result);
	}
}
